package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepCategory;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetail;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepCategoryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentUpkeepCategoryController}", description = "保养类别")
@RestController
@RequestMapping("/erp/equipment/upkeep/category/")
public class EquipmentUpkeepCategoryController {

    @Autowired
    private EquipmentUpkeepCategoryService equipmentUpkeepCategoryService;


    @GetMapping("list")
    @Operation(summary = "保养类别列表", description = "获取保养类别列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepCategory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepCategory> list = equipmentUpkeepCategoryService.list(EquipmentUpkeepCategory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "保养类别列表", description = "获取保养类别列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepCategory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentUpkeepCategory> pageResult = equipmentUpkeepCategoryService.listPage(EquipmentUpkeepCategory.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取保养类别列表", description = "根据ID获取保养类别列表", method = "GET")
    public Result<EquipmentUpkeepCategory> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentUpkeepCategory equipmentUpkeepCategory = equipmentUpkeepCategoryService.getById(EquipmentUpkeepCategory.class, id);
        return Result.OK(equipmentUpkeepCategory);
    }

    @RequestMapping("add")
    @Operation(summary = "保存保养类别", description = "保存保养类别")
    public Result<EquipmentUpkeepCategory> add(@RequestBody EquipmentUpkeepCategory equipmentUpkeepCategory) {
        equipmentUpkeepCategoryService.save(equipmentUpkeepCategory,new SmartCrudPolicyAdapter(){
            @Override
            public boolean beforeInsert(CrudContext context) {
                Object o = context.getEntity();
                if (o instanceof EquipmentUpkeepDetail) {
                    EquipmentUpkeepCategory parent = context.getParent();
                    ((EquipmentUpkeepDetail) o).setCategoryId(parent.getId());
                }
                return true;
            }
        });
        return Result.OK(equipmentUpkeepCategory);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑保养类别", description = "编辑保养类别", method = "PUT")
    public Result<EquipmentUpkeepCategory> edit(@RequestBody EquipmentUpkeepCategory equipmentUpkeepCategory) {
        equipmentUpkeepCategoryService.update(equipmentUpkeepCategory);
        return Result.OK(equipmentUpkeepCategory);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除保养类别", description = "删除保养类别,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentUpkeepCategory> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentUpkeepCategoryService.delete(EquipmentUpkeepCategory.class, key, new SmartCrudPolicyAdapter() {
                @Override
                public boolean beforeDelete(CrudContext context) {
                    Object o = context.getEntity();
                    if (o instanceof EquipmentUpkeepCategory){
                        try {
                            JpaUtil.lind(EquipmentUpkeepDetail.class).equal("categoryId", ((EquipmentUpkeepCategory) o).getId()).delete();
                        } catch (Exception e) {
                            return false;
                        }
                    }
                    return true;
                }
            });
        }
        return Result.OK();
    }


}
